//
//  ZBStatusOriginalView.swift
//  ZBWb
//
//  Created by 天隐翎 on 16/4/16.
//  Copyright © 2016年 csdn. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let StatusCellMargin: CGFloat = 8

class ZBStatusOriginalView: UIView {
    
    
    var bottomCons : Constraint?
//    重写Viewmodel
    var StatusCellViewModel:ZBStatusCellViewModel?{
        didSet{
            iconView.sd_setImageWithURL(StatusCellViewModel?.headImageURL)
            nameLabel.text = StatusCellViewModel?.status?.user?.name
            mbRankImageView.image = StatusCellViewModel?.mbImage
            verifiedImageView.image = StatusCellViewModel?.verifiedImage
            contentLabel.text = StatusCellViewModel?.status?.text
            
//            配图
//            pictureView.imageURLs = StatusCellViewModel?.status?.pic_urls
            
            //配图视图设置数据源
            self.bottomCons?.uninstall()
            if let urls = StatusCellViewModel?.status?.pic_urls where urls.count > 0 {
                //有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //设置数据源
                pictureView.imageURLs = urls
                //显示配图视图
                pictureView.hidden = false
            } else {
                //没有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //设置数据源 nil
                pictureView.imageURLs = nil
                //隐藏配图视图
                pictureView.hidden = true
                
            }

            
        }

    }

   override init(frame: CGRect){
        super.init(frame: frame)
//    设置UI
    setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        sepView.backgroundColor = UIColor.redColor()
//        添加子控件
        //圆角的处理
        iconView.cornerRadius = 20
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(sepView)
//        图片视图
        addSubview(pictureView)
        
//        底部条
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
//        设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
//        自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
//    懒加载控件
    /// 分隔条
    private lazy var sepView: UIView = UIView()
    
    
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 用户等级
    private lazy var mbRankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    /// 用户认证类型
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    /// 用户名
    private lazy var nameLabel: UILabel = UILabel(text: "15期的童鞋们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    /// 微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "15:15", textColor: UIColor.orangeColor(), fontSize: 12)
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12)
    /// 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "我没有男胖呀", textColor: UIColor.darkGrayColor(), fontSize: 15, margin: StatusCellMargin)
    
    
    private lazy var pictureView:ZBStatusPictureView = ZBStatusPictureView()
    
    

}
